﻿#include <QIcon>
#include <QPixmap>
#include <QDesktopServices>
#include <QUrl>
#include "CTableWidgetB.h"
#include "CGlobal.h"
#include "Public.h"

CTableWidgetB::CTableWidgetB(QWidget * parent)
{
	m_nDataType = 0;
	verticalHeader()->setHidden(true);
	setShowGrid(false);
	horizontalHeader()->setStretchLastSection(true);
	setSelectionBehavior(QAbstractItemView::SelectRows);
	setSelectionMode(QAbstractItemView::SingleSelection);
	setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
	setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
}

CTableWidgetB::~CTableWidgetB()
{
}

void CTableWidgetB::InitUi(int nType)//初始化界面
{
	m_nDataType = nType;
	clear();
	QStringList headerText;
	
	if (nType == 1)//跑偏报警记录查询
	{
		setColumnCount(7);
		headerText << A2T("序号") << A2T("摄像头名") << A2T("报警类型") << A2T("报警时间")<<A2T("报警等级")<<A2T("是否确认")<< A2T("操作");
		setColumnWidth(0, 50);
		setColumnWidth(1, 180);
		setColumnWidth(2, 150);
		setColumnWidth(3, 180);
		setColumnWidth(4, 120);
		setColumnWidth(5, 100);
		setColumnWidth(6, 120);
	}
	if (nType == 2)//跑偏报警记录查询
	{
		setColumnCount(7);
		headerText << A2T("序号") << A2T("摄像头名") << A2T("报警类型") << A2T("报警时间") << A2T("报警等级") << A2T("是否确认") << A2T("操作");
		setColumnWidth(0, 100);
		setColumnWidth(1, 280);
		setColumnWidth(2, 280);
		setColumnWidth(3, 200);
		setColumnWidth(4, 120);
		setColumnWidth(5, 200);
	}
	if (nType == 3)//报告结果记录查询
	{
		setColumnCount(5);
		headerText << A2T("序号") << A2T("报告名称") << A2T("生成时间") << A2T("路径") << A2T("操作");
		setColumnWidth(0, 100);
		setColumnWidth(1, 280);
		setColumnWidth(2, 280);
		setColumnWidth(3, 520);
	}
	setHorizontalHeaderLabels(headerText);
}

void CTableWidgetB::InitData(int nType, QVector<QVector<QString>>vecStr)
{
	setUpdatesEnabled(false); // 开始批量更新前禁用更新
	if (nType == 1 || nType == 2)
	{
		setRowCount(vecStr.size());
		for (int i = 0; i < vecStr.size(); i++)
		{
			setRowHeight(i, 30);
			if (vecStr[i].size() < 7)
			{
				continue;
			}
			MyStructViderAlarmItem cMyStructItem;
			cMyStructItem.m_pLabIndex->setText(vecStr[i][0]);//序号
			cMyStructItem.m_pCamerName->setText(vecStr[i][1]);
			cMyStructItem.m_pAlarmType->setText(vecStr[i][2]);
			cMyStructItem.m_pAlarmTime->setText(vecStr[i][3]);
			cMyStructItem.m_pAlarmLevel->setText(vecStr[i][4]);
			cMyStructItem.m_pIsConfirm->setText(vecStr[i][5]);
			cMyStructItem.m_pBtnView->setProperty("Guid", vecStr[i][6]);
			setCellWidget(i, 0, cMyStructItem.m_pLabIndex);
			setCellWidget(i, 1, cMyStructItem.m_pCamerName);
			setCellWidget(i, 2, cMyStructItem.m_pAlarmType);
			setCellWidget(i, 3, cMyStructItem.m_pAlarmTime);
			setCellWidget(i, 4, cMyStructItem.m_pAlarmLevel);
			setCellWidget(i, 5, cMyStructItem.m_pIsConfirm);
			setCellWidget(i, 6, cMyStructItem.m_pBtnView);
			connect(cMyStructItem.m_pBtnView, SIGNAL(clicked()), this, SLOT(slotClickedShowImg()));
		}
	}
	else if (nType == 3)
	{
		setRowCount(vecStr.size());
		for (int i = 0; i < vecStr.size(); i++)
		{
			setRowHeight(i, 30);
			if (vecStr[i].size() < 5)
			{
				continue;
			}
			MyStructResultItem cMyStructItem;
			cMyStructItem.m_pLabIndex->setText(vecStr[i][0]);//序号
			cMyStructItem.m_pResultName->setText(vecStr[i][1]);
			cMyStructItem.m_pReslutTime->setText(vecStr[i][2]);
			cMyStructItem.m_pPath->setText(vecStr[i][3]);
			cMyStructItem.m_pBtnView->setProperty("Path", vecStr[i][4]);
			setCellWidget(i, 0, cMyStructItem.m_pLabIndex);
			setCellWidget(i, 1, cMyStructItem.m_pResultName);
			setCellWidget(i, 2, cMyStructItem.m_pReslutTime);
			setCellWidget(i, 3, cMyStructItem.m_pPath);
			setCellWidget(i, 4, cMyStructItem.m_pBtnView);
			connect(cMyStructItem.m_pBtnView, SIGNAL(clicked()), this, SLOT(slotClickedShowResult()));
		}
	}
	setUpdatesEnabled(true); // 处理完成后启用更新
	update(); // 触发一次更新
}

void CTableWidgetB::slotClickedShowImg()
{
	QPushButton *btn = (QPushButton *)sender();
	QString sHttpUrl = btn->property("Guid").toString();//报警图片
	emit sigShowImg(sHttpUrl);
}

void CTableWidgetB::slotClickedShowResult()
{
	QPushButton *btn = (QPushButton *)sender();
	QString sPath = btn->property("Path").toString();
	emit sigShowResult(sPath);
}
